package com.wwj.controller.admin;


import com.wwj.common.constant.JwtClaimsConstant;
import com.wwj.common.properties.JwtProperties;
import com.wwj.common.result.PageResult;
import com.wwj.common.result.Result;
import com.wwj.common.utils.JwtUtil;
import com.wwj.pojo.dto.EmployeeDTO;
import com.wwj.pojo.dto.EmployeeLoginDTO;
import com.wwj.pojo.dto.EmployeePageQueryDTO;
import com.wwj.pojo.entity.Employee;
import com.wwj.pojo.vo.EmployeeLoginVO;
import com.wwj.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags="管理员员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;


    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }



    @PostMapping
    @ApiOperation(value = "新增管理员")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {

        log.info("新增员工:{}",employeeDTO);
        System.out.println("当前EmployeeController线程的id"+Thread.currentThread().getId());
        employeeService.save(employeeDTO);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除管理员员工")
    public Result deleteEmployee(@PathVariable Long id){
        employeeService.deleteEmployee(id);
        return Result.success();
    }


    @PutMapping
    @ApiOperation(value = "编辑管理员员工")
    public Result updateEmployee(@RequestBody EmployeeDTO employeeDTO) {


        employeeService.updateEmployee(employeeDTO);

        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "管理员员工分页查询")
    public Result<PageResult> Page(EmployeePageQueryDTO employeePageQueryDTO){

        PageResult pageResult= employeeService.pageQuery(employeePageQueryDTO);

        return Result.success(pageResult);

    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用,禁用管理员员工账号")
    public Result status(@PathVariable  Integer status, Long id) {
        log.info("启用,禁用管理员员工账号,{}",id);
        employeeService.updateByIdAndStatus(status,id);
        return Result.success();
    }

}
